package com.itany.ems.controller;

import com.itany.ems.entity.Emp;
import com.itany.ems.service.EmpService;
import com.itany.ems.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 石小俊
 * @date 2023年07月18日 15:58
 */
@WebServlet("*.emp")
public class EmpServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/findAll.emp".equals(path)){
            findAll(request,response);
        }
    }

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        try {
            List<Emp> emps = empService.findAll();
            request.setAttribute("emps",emps);
        } catch (Exception e) {
            request.setAttribute("empMsg","服务器内部异常");
        }
        request.getRequestDispatcher("/empList.jsp").forward(request,response);
    }
}
